/*
 * support3
 */
package com.pitop.errorcode.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ErrorcodePoolException;
import com.pitop.errorcode.dao.ErrorcodePoolDAO;
import com.pitop.errorcode.entity.ErrorcodePool;
import com.pitop.errorcode.entity.ErrorcodePoolState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("errorcodePoolSV")
@CacheConfig(cacheNames = "Support:ErrorcodePool_Cache")
@Slf4j
public class ErrorcodePoolSVImpl extends BaseSVImpl<ErrorcodePool, Long> implements ErrorcodePoolSV {

    @Autowired
    private ErrorcodePoolDAO errorcodePoolDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return errorcodePoolDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    @CacheEvict(allEntries = true)
    public void save(ErrorcodePool entity) throws BaseException {
        entity.setState(ErrorcodePoolState.Enable.name());
        super.save(entity);
    }

    /**
     * 数据更新
     *
     * @param errorcodePool 错误码实体
     */
    @Override
    @CacheEvict(allEntries = true)
    public void modify(ErrorcodePool errorcodePool) {
        getBaseDAO().update(errorcodePool);
    }

    /**
     * 加载一个对象ErrorcodePool
     *
     * @param id * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool load(Long id, String errorCode) {
        if (id == null && errorCode == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorCode", errorCode);
        return errorcodePoolDAO.load(param);
    }

    /**
     * 加载一个对象ErrorcodePool 通过id
     *
     * @param id
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool loadById(Long id) {
        if (id == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return errorcodePoolDAO.loadById(id);
    }

    /**
     * 加载一个对象ErrorcodePool 通过errorCode
     *
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    public ErrorcodePool loadByErrorCode(String errorCode) {
        if (errorCode == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return errorcodePoolDAO.loadByErrorCode(errorCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 ErrorcodePool 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateStateById(Long id, ErrorcodePoolState newState, ErrorcodePoolState... oldStates) {
        if (id == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errorcodePoolDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键errorCode,oldStates 共同更新 ErrorcodePool 的状态到newState状态
     *
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateStateByErrorCode(String errorCode, ErrorcodePoolState newState, ErrorcodePoolState... oldStates) {
        if (errorCode == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        errorcodePoolDAO.updateStateByErrorCode(errorCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 ErrorcodePool 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateById(Long id, ErrorcodePoolState state) {
        if (id == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        errorcodePoolDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键errorCode 更新 ErrorcodePool 的状态到另一个状态
     *
     * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @param state     状态
     */
    @Override
    @CacheEvict(allEntries = true)
    public void updateByErrorCode(String errorCode, ErrorcodePoolState state) {
        if (errorCode == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        errorcodePoolDAO.updateByErrorCode(errorCode, state.name(), new Date());
    }


    /**
     * 删除对象ErrorcodePool
     *
     * @param id * @param errorCode 错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @return ErrorcodePool
     */
    @Override
    @CacheEvict(allEntries = true)
    public void delete(Long id, String errorCode) {
        if (id == null && errorCode == null) {
            throw new ErrorcodePoolException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorCode", errorCode);
        errorcodePoolDAO.delete(param);
    }


    /**
     * 查询ErrorcodePool分页
     *
     * @param errorcodePool 对象
     * @param offset        查询开始行
     * @param limit         查询行数
     * @return List<ErrorcodePool>
     */
    @Override
//    @Cacheable(key = "'list'")
    public List<ErrorcodePool> list(ErrorcodePool errorcodePool, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (errorcodePool != null) {
            map = JSON.parseObject(JSON.toJSONString(errorcodePool, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return errorcodePoolDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ErrorcodePool errorcodePool) {
        Map<String, Object> map = null;
        if (errorcodePool != null) {
            map = JSON.parseObject(JSON.toJSONString(errorcodePool, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return errorcodePoolDAO.count(map);
    }

    /**
     * 查询ErrorcodePool分页
     *
     * @param id     * @param errorCode  错误码 由人为定义并制定出来指定一定场景模块的错误编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ErrorcodePool>
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#id).concat('-').concat(#code).concat('-').concat('list')")
    public List<ErrorcodePool> list(Long id, String errorCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorCode", errorCode);
        return errorcodePoolDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String errorCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("errorCode", errorCode);
        return errorcodePoolDAO.count(param);
    }

    /**
     * 修改状态
     *
     * @param errorCode 错误码库编码
     * @param state     状态
     * @return errorCodePool
     */
    @Override
    public void updateState(String errorCode, ErrorcodePoolState state) {
        Map<String, Object> params = new HashedMap();
        params.put("errorCode", errorCode);
        params.put("state", state.name());
        errorcodePoolDAO.update(params);
    }

}
